
package com.kanjian.music.adapter;

import java.util.ArrayList;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Bitmap.Config;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader.ImageListener;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.ImageLoader.ImageContainer;
import com.kanjian.music.R;
import com.kanjian.music.activity.BaseActivity;
import com.kanjian.music.activity.MusicianHomeActivity;
import com.kanjian.music.activity.SubscriptionActivity;
import com.kanjian.music.constants.IntentConstants;
import com.kanjian.music.entity.Music;
import com.kanjian.music.entity.Musician;
import com.kanjian.music.entity.Recommend;
import com.kanjian.music.util.FastBlur;
import com.kanjian.music.util.PixUtil;
import com.kanjian.music.util.PlayerUtil;
import com.kanjian.music.view.NoScrollGridView;
import com.kanjian.music.view.RecyclingImageView;
import com.kanjian.music.volley.VolleyQueue;

/**
 * 推荐页ListAdapter
 * 
 * @author xuyanjun@kanjian.com
 */
public class RecommendListAdapter extends MusicSubFragmentListAdapater<Recommend> {

    private final static int VIEW_TYPE_COUNT = 5;

    private final static int VIEW_TYPE_TITLE = 1;

    private final static int VIEW_TYPE_SUBSCRIPTION = 2;

    private final static int VIEW_TYPE_MUSICIAN = 3;

    private final static int VIEW_TYPE_MUSIC = 4;

    private RecommendMusicianGridAdapter mMusicianGridAdapter;

    private BaseActivity mActivity;

    public RecommendListAdapter(BaseActivity context) {
        super(context);
        mActivity = context;
    }

    public void registerOnClickListener(ListView listView) {
        listView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Recommend recommend = getItem(position);
                if (recommend != null) {
                    if (recommend.mType == Recommend.RECOMMEND_TYPE_RECOMMEND_MUSIC) {
                        if (recommend.mMusic != null) {
                            PlayerUtil.addMusicThenPlay(recommend.mMusic, mActivity.mPlayService);
                        }
                    } else if (recommend.mType == Recommend.RECOMMEND_TYPE_SUBSCRIPTION) {
                        Recommend neighborRecommend;
                        Intent intent = new Intent();
                        intent.setClass(mActivity, SubscriptionActivity.class);
                        if (recommend.mSubscriptionMusic != null) {
                            neighborRecommend = getItem(position + 1);
                            // click subscriptionmusic
                            Music firstMusic = recommend.mSubscriptionMusic.get(0);
                            if (firstMusic != null && firstMusic.getMusicId() == -1) {
                                // 缺省状态 还未加载完成
                                return;
                            } else if (firstMusic != null && firstMusic.getMusicId() == -2) {
                                // 未登录 或其他原因 没有拿到推荐歌曲
                                
                                return;
                            }
                            intent.putExtra(IntentConstants.MUSICLIST, recommend.mSubscriptionMusic);
                            // 推荐音乐人在推荐歌曲下一个index
                            intent.putExtra(IntentConstants.MUSICIANLIST,
                                    neighborRecommend.mSubscriptionMusician);
                            intent.putExtra(IntentConstants.SUBSCRIPTION_FOCUS,
                                    SubscriptionActivity.SUBSCRIPTION_FOCUS_MUSICLIST);
                        } else if (recommend.mSubscriptionMusician != null) {
                            intent.putExtra(IntentConstants.MUSICLIST, recommend.mSubscriptionMusic);
                            neighborRecommend = getItem(position - 1);
                            // click subscriptionmusician
                            Musician firstMusician = recommend.mSubscriptionMusician.get(0);
                            if (firstMusician != null && firstMusician.mUserId == -1) {
                                // 缺省状态
                                return;
                            } else if (firstMusician != null && firstMusician.mUserId == -2) {
                                // 未登录 或其他原因 没有拿到推荐音乐人
                                
                                return;
                            }
                            intent.putExtra(IntentConstants.MUSICIANLIST,
                                    recommend.mSubscriptionMusician);
                            // 推荐歌曲在推荐音乐人上一个index
                            intent.putExtra(IntentConstants.MUSICLIST,
                                    neighborRecommend.mSubscriptionMusic);
                            intent.putExtra(IntentConstants.SUBSCRIPTION_FOCUS,
                                    SubscriptionActivity.SUBSCRIPTION_FOCUS_MUSICIANLIST);
                        }

                        mActivity.startActivityWithAnimation(intent);
                    }
                }
            }
        });
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return VIEW_TYPE_FAKE;
        } else {
            switch (getItem(position).mType) {
                case Recommend.RECOMMEND_TYPE_RECOMMEND_MUSIC:
                    return VIEW_TYPE_MUSIC;
                case Recommend.RECOMMEND_TYPE_RECOMMEND_MUSICIAN:
                    return VIEW_TYPE_MUSICIAN;
                case Recommend.RECOMMEND_TYPE_RECOMMEND_TITLE:
                    return VIEW_TYPE_TITLE;
                case Recommend.RECOMMEND_TYPE_SUBSCRIPTION:
                    return VIEW_TYPE_SUBSCRIPTION;
                default:
                    return VIEW_TYPE_MUSIC;
            }
        }
    }

    @Override
    public int getViewTypeCount() {
        return VIEW_TYPE_COUNT;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        super.getView(position, convertView, parent);
        if (position == 2) {
            System.out.println(position);
        }
        int viewType = getItemViewType(position);
        if (viewType == VIEW_TYPE_FAKE) {
            return mListFakeView;
        } else {
            ViewHolder holder;
            if (convertView == null
                    || convertView.getTag(getTagResoureIDFromViewType(viewType)) == null) {
                if (viewType == VIEW_TYPE_TITLE) {
                    holder = new RecommendTitleViewHolder();
                    RecommendTitleViewHolder tHolder = (RecommendTitleViewHolder) holder;
                    convertView = mLayoutInflater.inflate(R.layout.item_list_recommend_title, null);
                    tHolder.mTv_recommendTitle = (TextView) convertView
                            .findViewById(R.id.item_list_recommend_title);
                } else if (viewType == VIEW_TYPE_SUBSCRIPTION) {
                    holder = new RecommendSubscriptionViewHolder();
                    RecommendSubscriptionViewHolder tHolder = (RecommendSubscriptionViewHolder) holder;
                    convertView = mLayoutInflater.inflate(
                            R.layout.item_list_recommend_subscription, null);
                    tHolder.mIv_Pic = (NetworkImageView) convertView
                            .findViewById(R.id.item_list_recommend_subscription_pic);
                    tHolder.mIv_Pic.setDefaultImageResId(R.drawable.default_image_small);
                    tHolder.mIv_Pic.setErrorImageResId(R.drawable.default_image_small);
                    tHolder.mIv_background = (RecyclingImageView) convertView
                            .findViewById(R.id.item_list_recommend_subscription_background);

                    tHolder.mTv_title = (TextView) convertView
                            .findViewById(R.id.item_list_recommend_subscription_title);
                    tHolder.mTv_subTitle = (TextView) convertView
                            .findViewById(R.id.item_list_recommend_subscription_subtitle);

                } else if (viewType == VIEW_TYPE_MUSICIAN) {
                    holder = new RecommendMusicianViewHolder();
                    RecommendMusicianViewHolder tHolder = (RecommendMusicianViewHolder) holder;
                    convertView = mLayoutInflater.inflate(R.layout.item_list_recommend_musician,
                            null);
                    tHolder.mGv_musican = (NoScrollGridView) convertView
                            .findViewById(R.id.item_list_recommend_musician_gird);

                } else {
                    holder = new RecommendMusicViewHolder();
                    RecommendMusicViewHolder tHolder = (RecommendMusicViewHolder) holder;
                    convertView = mLayoutInflater.inflate(R.layout.item_list_recommend_music, null);
                    tHolder.mIv_musicPic = (NetworkImageView) convertView
                            .findViewById(R.id.item_list_musiclist_pic);
                    tHolder.mIv_musicPic.setDefaultImageResId(R.drawable.default_image_small);
                    tHolder.mIv_musicPic.setErrorImageResId(R.drawable.default_image_small);
                    tHolder.mTv_musicName = (TextView) convertView
                            .findViewById(R.id.item_list_musiclist_musicname);
                    tHolder.mTv_Songer = (TextView) convertView
                            .findViewById(R.id.item_list_musiclist_songer);
                }

                convertView.setTag(getTagResoureIDFromViewType(viewType), holder);
            } else {
                holder = (ViewHolder) convertView.getTag(getTagResoureIDFromViewType(viewType));
            }

            Recommend recommend = getItem(position);

            if (viewType == VIEW_TYPE_TITLE) {
                RecommendTitleViewHolder tHolder = (RecommendTitleViewHolder) holder;
                tHolder.mTv_recommendTitle.setText(recommend.mTitle);

            } else if (viewType == VIEW_TYPE_SUBSCRIPTION) {
                final RecommendSubscriptionViewHolder tHolder = (RecommendSubscriptionViewHolder) holder;

                /*
                 * 撑开背景图到100dp 否则不会填满整个listItem 100dp为userpic的高度
                 */
                LayoutParams l = tHolder.mIv_background.getLayoutParams();
                l.height = (int) PixUtil.getRawSize(TypedValue.COMPLEX_UNIT_DIP, 100);
                tHolder.mIv_background.setLayoutParams(l);
                if (recommend.mSubscriptionMusic != null) {
                    tHolder.mTv_title.setText("你可能喜欢的歌曲");
                    tHolder.mTv_subTitle.setText("根据你的音乐基因为你推荐歌曲");
                    Music firstMusic = recommend.mSubscriptionMusic.get(0);
                    // 默认
                    if (firstMusic.getMusicId() == -1) {
                        if (VolleyQueue.mBitMapCache.getBitmap("11111111111") == null) {
                            Bitmap bmp = FastBlur.doBlur(BitmapFactory.decodeResource(
                                    mActivity.getResources(), R.drawable.image_test), 20, false);
                            VolleyQueue.mBitMapCache.putBitmap("11111111111", bmp);
                            tHolder.mIv_background.setImageBitmap(bmp);

                        } else {
                            tHolder.mIv_background.setImageBitmap(VolleyQueue.mBitMapCache
                                    .getBitmap("11111111111"));
                        }
                    } else {
                        VolleyQueue.getImageRequestQueue().add(
                                new ImageRequest(firstMusic.getUserPic(),
                                        new Response.Listener<Bitmap>() {
                                            @Override
                                            public void onResponse(Bitmap bmp) {
                                                tHolder.mIv_background.setImageBitmap(FastBlur
                                                        .doBlur(bmp, 20, false));
                                            }
                                        }, 0, 0, Config.ARGB_8888, null));
                        tHolder.mIv_Pic.setImageUrl(firstMusic.getUserPic(),
                                VolleyQueue.getImageLoader());
                    }

                } else if (recommend.mSubscriptionMusician != null) {
                    tHolder.mTv_title.setText("你可能喜欢的音乐人");
                    tHolder.mTv_subTitle.setText("根据你的音乐基因为你推荐音乐人");
                    Musician firstMusician = recommend.mSubscriptionMusician.get(0);

                    if (firstMusician.mUserId == -1) {
                        if (VolleyQueue.mBitMapCache.getBitmap("11111111111") == null) {
                            Bitmap bmp = FastBlur.doBlur(BitmapFactory.decodeResource(
                                    mActivity.getResources(), R.drawable.image_test), 20, false);
                            VolleyQueue.mBitMapCache.putBitmap("11111111111", bmp);
                            tHolder.mIv_background.setImageBitmap(bmp);

                        } else {
                            tHolder.mIv_background.setImageBitmap(VolleyQueue.mBitMapCache
                                    .getBitmap("11111111111"));
                        }
                    } else {
                        VolleyQueue.getImageRequestQueue().add(
                                new ImageRequest(firstMusician.mUserPic,
                                        new Response.Listener<Bitmap>() {
                                            @Override
                                            public void onResponse(Bitmap bmp) {
                                                tHolder.mIv_background.setImageBitmap(FastBlur
                                                        .doBlur(bmp, 20, false));
                                            }
                                        }, 0, 0, Config.ARGB_8888, null));
                        tHolder.mIv_Pic.setImageUrl(firstMusician.mUserPic,
                                VolleyQueue.getImageLoader());
                    }

                }
            } else if (viewType == VIEW_TYPE_MUSICIAN) {
                RecommendMusicianViewHolder tHolder = (RecommendMusicianViewHolder) holder;
                mMusicianGridAdapter = new RecommendMusicianGridAdapter();
                mMusicianGridAdapter.registerOnItemClickListener(tHolder.mGv_musican);
                mMusicianGridAdapter.setData(recommend.mMusician);
                tHolder.mGv_musican.setAdapter(mMusicianGridAdapter);

            } else {
                RecommendMusicViewHolder tHolder = (RecommendMusicViewHolder) holder;
                tHolder.mIv_musicPic.setImageUrl(recommend.mMusic.getUserPic(),
                        VolleyQueue.getImageLoader());
                tHolder.mTv_musicName.setText(recommend.mMusic.getMusicName());
                tHolder.mTv_Songer.setText(recommend.mMusic.getUserName());

            }

            return convertView;
        }
    }

    private int getTagResoureIDFromViewType(int viewType) {
        switch (viewType) {
            case VIEW_TYPE_TITLE:
                return Recommend.RECOMMEND_TYPE_RECOMMEND_TITLE;
            case VIEW_TYPE_SUBSCRIPTION:
                return Recommend.RECOMMEND_TYPE_SUBSCRIPTION;
            case VIEW_TYPE_MUSICIAN:
                return Recommend.RECOMMEND_TYPE_RECOMMEND_MUSICIAN;
            case VIEW_TYPE_MUSIC:
                return Recommend.RECOMMEND_TYPE_RECOMMEND_MUSIC;
            default:
                return Recommend.RECOMMEND_TYPE_RECOMMEND_MUSIC;
        }
    }

    static class ViewHolder {
    }

    static class RecommendTitleViewHolder extends ViewHolder {
        TextView mTv_recommendTitle;
    }

    static class RecommendSubscriptionViewHolder extends ViewHolder {
        NetworkImageView mIv_Pic;

        RecyclingImageView mIv_background;

        TextView mTv_title;

        TextView mTv_subTitle;
    }

    static class RecommendMusicianViewHolder extends ViewHolder {

        NoScrollGridView mGv_musican;

        // NetworkImageView mIv_musicianPic;
        //
        // TextView mTv_musicianName;
        //
        // TextView mTv_musicianGenre;
    }

    static class RecommendMusicViewHolder extends ViewHolder {
        NetworkImageView mIv_musicPic;

        TextView mTv_musicName;

        TextView mTv_Songer;
    }

    class RecommendMusicianGridAdapter extends BaseAdapter {

        private ArrayList<Musician> mMusicianList;

        public RecommendMusicianGridAdapter() {
        }

        public void registerOnItemClickListener(GridView gv) {
            gv.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Musician musician = getItem(position);
                    if (musician != null) {
                        Intent intent = new Intent();
                        intent.putExtra(IntentConstants.MUSICIANID, musician.mUserId);
                        intent.setClass(mActivity, MusicianHomeActivity.class);
                        mActivity.startActivityWithAnimation(intent);
                    }
                }
            });
        }

        public void setData(ArrayList<Musician> musicianList) {
            if (this.mMusicianList != null) {
                this.mMusicianList.clear();
                this.mMusicianList.addAll(musicianList);
            } else {
                this.mMusicianList = musicianList;
            }
        }

        public RecommendMusicianGridAdapter(ArrayList<Musician> musicianList) {
            this.mMusicianList = musicianList;
        }

        @Override
        public int getCount() {
            return mMusicianList != null ? mMusicianList.size() : 0;
        }

        @Override
        public Musician getItem(int position) {
            return mMusicianList != null ? mMusicianList.get(position) : null;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null || convertView.getTag() == null) {
                holder = new ViewHolder();
                convertView = mLayoutInflater.inflate(R.layout.item_grid_recommend_musician, null);
                holder.mIv_musicianPic = (NetworkImageView) convertView
                        .findViewById(R.id.item_grid_recommend_musician_pic);
                holder.mIv_musicianPic.setDefaultImageResId(R.drawable.default_image_small);
                holder.mTv_musicianName = (TextView) convertView
                        .findViewById(R.id.item_grid_recommend_musician_name);
                holder.mTv_musicianGenre = (TextView) convertView
                        .findViewById(R.id.item_grid_recommend_musician_genre);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            Musician musician = getItem(position);
            holder.mIv_musicianPic.setImageUrl(musician.mUserPic, VolleyQueue.getImageLoader());
            holder.mTv_musicianName.setText(musician.mUserName);
            // holder.mTv_musicianGenre.setText(musician.mGenre.toString());
            return convertView;
        }

        class ViewHolder {
            NetworkImageView mIv_musicianPic;

            TextView mTv_musicianName;

            TextView mTv_musicianGenre;
        }

    }

}
